package com.leyou.web;

import com.leyou.common.vo.PageResultVO;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.security.PublicKey;
import java.util.List;
import java.util.Map;


/**
 * @author xiuer
 * @description 商品控制层
 * @create 2019/6/27 21:27
 */
@RestController
public class GoodsController {

    @Autowired
    private GoodsService goodsService;
    //http://api.leyou.com/api/item/spu/page?key=&saleable=true&page=1&rows=5
    // key:
    // saleable: true
    // page: 1
    // rows: 5

    /**
     * 分页查询spu
     *
     * @param key      商品搜索的条件
     * @param saleable 是否上架
     * @param page     当前页数
     * @param rows     每页显示的条数
     * @return 分页数据
     */
    @GetMapping("spu/page")
    public ResponseEntity<PageResultVO<SpuDTO>> goodsQueryPage(
            @RequestParam(value = "key", required = false) String key,
            @RequestParam(value = "saleable", required = false) Boolean saleable,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows) {

        return ResponseEntity.ok(goodsService.goodsQueryPage(key, saleable, page, rows));

    }

    /**
     * 根据spuId查询spu
     */
    @GetMapping("spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(goodsService.querySpuById(id));
    }

    /**
     * 新增商品 （sku spu spu_detail）
     *
     * @param spuDTO
     * @return
     */
    @PostMapping("goods")
    public ResponseEntity<Void> insertGoods(@RequestBody SpuDTO spuDTO) {
        goodsService.insertGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 更新商品 （sku spu spu_detail）
     */
    @PutMapping("goods")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO) {
        goodsService.updateGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 下架商品（）
     * http://api.leyou.com/api/item/spu/saleable
     *
     * @Param id Sku主键
     * @Param saleable 上下架状态
     */
    @PutMapping("spu/saleable")
    public ResponseEntity<Void> updateSaleableBySkuId(@RequestParam("id") Long id,
                                                      @RequestParam("saleable") Boolean saleable) {
        goodsService.updateSaleableBySkuId(id, saleable);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    /**
     * 根据 spu_id查询 spueDtail
     * //http://api.leyou.com/api/item/spu/detail?id=199
     *
     * @param id spu_id
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> selectSpuDetail(@RequestParam("id") Long id) {
        return ResponseEntity.ok(goodsService.selectSpuDetail(id));
    }

    /**
     * 根据 spu_id查询 sku集合
     * http://api.leyou.com/api/item/sku/of/spu?id=199
     */
    @GetMapping("sku/of/spu")
    public ResponseEntity<List<SkuDTO>> selectSku(@RequestParam("id") Long id) {
        return ResponseEntity.ok(goodsService.selectSku(id));
    }

    /**
     * 根据sku_ids 查询sku集合
     * http://api.leyou.com/api/item/sku/list?ids=27359021545,13645340683,11319391377
     */
    @GetMapping("sku/list")
    public ResponseEntity<List<SkuDTO>> querySkuByIds(@RequestParam("ids") List<Long> ids) {
        return ResponseEntity.ok(goodsService.querySkuByIds(ids));
    }

    /**
     * 根据订单id和商品的数量减库存
     * @Param numCartMap key 为 skuId 值为 商品数量
     */
    @PutMapping("/stock/minus")
    public ResponseEntity<Void> minusStock(@RequestBody Map<Long, Integer> numCartMap) {
        goodsService.minusStock(numCartMap);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

}
